#include "SceneManager.h"
#include "../Scenes/Play/PlayManager.h"
#include "../Scenes/Level/LevelManager.h"
#include "../Scenes/Menu/MenuManager.h"
#include "../Scenes/Splash/SplashManager.h"
#include "../Scenes/Play/Layers/PlayCongratulationsLayer.h"
#include "CCActivityIndicator.h"
#define kChangeSceneDuration 1.0f
SceneManager* SceneManager::m_instance = nullptr;

SceneManager* SceneManager::getInstance()
{
	if (m_instance == nullptr)
		m_instance = new SceneManager();
	return m_instance;
}

SceneManager::SceneManager()
{
    m_screenSize = Director::getInstance()->getVisibleSize();
}

SceneManager::~SceneManager()
{
}

void SceneManager::changeScene(Node* layer, EScene scene)
{
	CallFuncN* callback = nullptr;
    CallFuncN* funcActive = CallFuncN::create(CC_CALLBACK_0(SceneManager::setActiveButton, this));
	float duration = 0.1f;

	switch (scene)
	{
	case kScene_Splash:
		callback = CallFuncN::create(CC_CALLBACK_0(SceneManager::goSplash, this));
		duration = 0.1f;
		break;
	case kScene_Menu:
		callback = CallFuncN::create(CC_CALLBACK_0(SceneManager::goMenu, this));
		duration = 0.1f;
		break;
	case kScene_Level:
		callback = CallFuncN::create(CC_CALLBACK_0(SceneManager::goLevel, this));
		duration = 0.1f;
		break;
	case kScene_Play:
		callback = CallFuncN::create(CC_CALLBACK_0(SceneManager::goPlay, this));
		duration = 2.0f;
		break;
	}
    CCActivityIndicator * activityIndicator = CCActivityIndicator::create();

    
    activityIndicator->setPosition(Vec2(m_screenSize.width/2,m_screenSize.height/2));
    layer->addChild(activityIndicator,999);
    activityIndicator->startAnimating();
	layer->runAction(Sequence::create(DelayTime::create(duration), callback,funcActive, NULL));
}

void SceneManager::goSplash()
{
	Director::getInstance()->replaceScene(TransitionFade::create((float)kChangeSceneDuration, SplashManager::createScene()));
}

void SceneManager::goMenu()
{
	Director::getInstance()->replaceScene(TransitionFade::create((float)kChangeSceneDuration, MenuManager::createScene()));
}

void SceneManager::goLevel()
{
	Director::getInstance()->replaceScene(TransitionFade::create((float)kChangeSceneDuration, LevelManager::createScene()));
}

void SceneManager::goPlay()
{
	Director::getInstance()->replaceScene(TransitionFade::create((float)kChangeSceneDuration, PlayManager::createScene()));
}

void SceneManager::setActiveButton()
{
    PlayCongratulationsLayer::isProcessing = false;
}